package anonymouschat.messages.clientrequests;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.IvParameterSpec;

import anonymouschat.messages.Message;

public class encryptedMessageWrapperMessage extends Message implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -5438981469645327144L;
	private String nextHop;
	private byte[] encryptedMessage;
	private boolean isAsymmetricEnc; //Is encrypted Asymetrically (pub/priv) or with Symmetric Key
	private byte[] iv;
	private Long connectionNumber = null;
	private UserLoginRequest login = null;
	/**
	 * Wraps an object in encryption 
	 * @param enc
	 * @param encryptedMessage
	 * @throws IllegalBlockSizeException
	 * @throws BadPaddingException
	 * @throws IOException 
	 */
	public encryptedMessageWrapperMessage(Cipher enc, Message encryptedMessage, boolean isAsymmetricEnc) throws IllegalBlockSizeException, BadPaddingException, IOException
	{
	  ByteArrayOutputStream bStream = new ByteArrayOutputStream();
	    ObjectOutputStream oStream = new ObjectOutputStream(bStream);
	    oStream.writeObject(encryptedMessage);
	  //this.encryptedMessage = enc.doFinal(bStream.toByteArray());
	  this.encryptedMessage = bStream.toByteArray();
	  this.isAsymmetricEnc = isAsymmetricEnc;
	  if (!isAsymmetricEnc)
		  iv = enc.getIV();
	}
	
	public Long getConnectionNumber()
	{
		return connectionNumber;
	}
	
	public void setConnectionNumber(long connectionNumber)
	{
		this.connectionNumber = connectionNumber;
	}
	
	public boolean isAsymmetricEncrypted()
	{
		return isAsymmetricEnc;
	}
	
	public IvParameterSpec getIV()
	{
		return new IvParameterSpec(iv);
	}
	
	public void setNextHop(String dest)
	{
		nextHop = dest;
	}
	
	/** 
	 * Returns the next hop in the annonymized routing chain
	 * @return
	 */
	public String getNextHop()
	{
		return nextHop;
	}
	
	public byte[] getencryptedMessage()
	{
		return encryptedMessage;
	}
	
	public void setUserLogin( UserLoginRequest login)
	{
	    this.login = login;
	}
	
	public UserLoginRequest getUserLogin()
	{
		return login;
	}
	
}
